﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Common.Library;
using System.ComponentModel;
using System.Windows.Navigation;
using OnlineVideos.UI;
using OnlineVideos.Common;
using OnlineVideos.Data;
using OnlineVideos.Utilities;
using Microsoft.Phone.Controls;
using Common.Utilities;
using OnlineVideos.Entities;
namespace OnlineVideos
{
    public partial class FavoriteShows : UserControl
    {
        #region GlobalDeclaration

        List<ShowList> showFavShows = null;
        string link = string.Empty;
        #endregion

        #region Constructor

        public FavoriteShows()
        {
            InitializeComponent();
            showFavShows = new List<ShowList>();
        }
        #endregion

        #region PageLoad

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            GetPageDataInBackground();
        }
        #endregion

        #region "Common Methods"

        private void GetPageDataInBackground()
        {
            try
            {
                BackgroundHelper bwHelper = new BackgroundHelper();
                bwHelper.AddBackgroundTask(
                                                   (object s, DoWorkEventArgs a) =>
                                                   {
                                                       a.Result = FavoritesManager.GetFavoriteShows();
                                                   },
                                                   (object s, RunWorkerCompletedEventArgs a) =>
                                                   {
                                                       showFavShows = (List<ShowList>)a.Result;
                                                       if (showFavShows.Count > 0)
                                                       {
                                                           lbxVidoes.ItemsSource = showFavShows;

                                                           tblkFavShows.Visibility = Visibility.Collapsed;
                                                       }
                                                       else
                                                       {
                                                           tblkFavShows.Text = "No " + AppResources.FavoriteMoviesPivotTitle + " available";
                                                           tblkFavShows.Visibility = Visibility.Visible;
                                                       }
                                                   }
                                                 );
                bwHelper.RunBackgroundWorkers();
            }
            catch (Exception ex)
            {
                
              Exceptions.SaveOrSendExceptions("Exception in GetPageDataInBackground Method In FavoriteShows.cs file.", ex);
            }
        }
        #endregion

        #region Events

        private void lbxMovies_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (lbxVidoes.SelectedIndex == -1)
                    return;
                AppSettings.LinkTitle = (lbxVidoes.SelectedItem as ShowLinks).Title.ToString();
                AppSettings.LinkType = (lbxVidoes.SelectedItem as ShowLinks).LinkType.ToString();
                //TODO: verify this logic
                if (ResourceHelper.ProjectName == AppResources.VideoMix || ResourceHelper.AppName==Apps.Web_Tile.ToString())
                {
                    Constants.navigationfrom ="FavoriteShows";
                    PageHelper.NavigateToDetailPage(AppResources.DetailPageName, AppSettings.ShowID);
                }
                else
                {
                    if (!AppResources.ShowFavoritesPageMoviesPivot)
                    {
                        List<ShowLinks> showLinks = OnlineShow.GetShowLinksByTypeForWp8((lbxVidoes.SelectedItem as ShowLinks).ShowID.ToString(), LinkType.Movies);
                        foreach (ShowLinks objLinks in showLinks)
                        {
                            link = objLinks.LinkUrl;
                        }
                        PageHelper.NavigateTo(NavigationHelper.youtubeBrowserPage(link));
                    }
                    else
                    {
                        if (!AppResources.IsSimpleDetailPage)
                        {
                            PhoneApplicationFrame frame = Application.Current.RootVisual as PhoneApplicationFrame;
                            Page p = frame.Content as Page;
                            Pivot p1 = (Pivot)(p.FindName("pvtMainFavorites"));
                            if (p1 != null)
                                    PageHelper.NavigateTo(NavigationHelper.getMovieDetailPage((lbxVidoes.SelectedItem as ShowList), p1.SelectedItem.GetType().GetProperty("Header").GetValue((object)p1.SelectedItem, null).ToString()));
                        }
                        else
                        {
                            PhoneApplicationFrame frame = Application.Current.RootVisual as PhoneApplicationFrame;
                            Page p = frame.Content as Page;
                            Pivot p1 = (Pivot)(p.FindName("pvtMainFavorites"));
                            if (p1 != null)
                            {
                                if(ResourceHelper.AppName==Apps.World_Dance.ToString())
                                    PageHelper.NavigateTo(NavigationHelper.getDanceDetailPage((lbxVidoes.SelectedItem as ShowList), p1.SelectedItem.GetType().GetProperty("Header").GetValue((object)p1.SelectedItem, null).ToString()));
                                else
                                PageHelper.NavigateTo(NavigationHelper.getMusicDetailPage((lbxVidoes.SelectedItem as ShowList), p1.SelectedItem.GetType().GetProperty("Header").GetValue((object)p1.SelectedItem, null).ToString()));
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in lbxMovies_SelectionChanged Method In FavoriteShows.cs file.", ex);
            }
            lbxVidoes.SelectedIndex = -1;
        }
        #endregion
    }
}
